package dream.web;

import dream.pojo.User;
import dream.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    private UserService service = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username").trim();
        String password = request.getParameter("password").trim();
        String password1 = request.getParameter("password1").trim();
        String code = request.getParameter("checkCode");

        System.out.println(username);
        System.out.println(password);
        System.out.println(password1);
        System.out.println(code);

        if(username.equals("")||password.equals("")||password1.equals("")||code.equals("")){
            // 存储错误信息到request
            request.setAttribute("register1_msg", "输入框不能为空或空格");

            //跳转到register.jsp
            request.getRequestDispatcher("/regin_new.jsp").forward(request, response);
            return;
        }

        if(username.length() < 8 || username.length() > 18){
            // 存储错误信息到request
            request.setAttribute("register_msg", "账号格式错误");

            //跳转到register.jsp
            request.getRequestDispatcher("//regin_new.jsp").forward(request, response);
            return;
        }

        if(!password.equals(password1)){
            // 存储错误信息到request
            request.setAttribute("register_msg", "两次密码不一致");

            //跳转到register.jsp
            request.getRequestDispatcher("/regin_new.jsp").forward(request, response);
            return;
        }

        if(!password.matches("^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$")){
            // 存储错误信息到request
            request.setAttribute("register_msg", "密码格式不对");

            //跳转到register.jsp
            request.getRequestDispatcher("/regin_new.jsp").forward(request, response);
            return;
        }

        //判断验证码是否正确
        //程序生成的验证码，从Session中获取
        HttpSession session = request.getSession();
        String checkCodeGen = (String) session.getAttribute("checkCodeGen");

        //对比
        if(!checkCodeGen.equalsIgnoreCase(code)){
            request.setAttribute("register_msg", "验证码错误");
            request.getRequestDispatcher("/regin_new.jsp").forward(request, response);
            return;
        }

        //封装对象
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        //调用service注册
        boolean flag = service.register(user);
        //判断注册成功与否
        if(flag){
            //注册成功，跳转到登录页面

            request.setAttribute("register_msg", "注册成功，请登录");
            request.getRequestDispatcher("/login_new.jsp").forward(request, response);
        }else{
            //注册失败，跳转到注册页面

            request.setAttribute("register_msg", "用户名已存在");
            request.getRequestDispatcher("/regin_new.jsp").forward(request, response);
        }

    }
}
